package yxy.game.pm2.active.command.actpay;

import org.joda.time.DateTime;
import yxy.cherry.data.bean.acttimelimit;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.ActiveModule_每日特惠;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActDailySpecialInfoRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@MessageType(proto = Command.ActDailySpecialInfo, classtype = ActiveMessage.ActDailySpecialInfoRequest.class)
public class CMD_Active_每日特惠_Info extends Command<ActDailySpecialInfoRequest> {

	@Override
	public void execute(MessageSource messageSource, ActDailySpecialInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActDailySpecialInfoResponse.Builder response = ActiveMessage.ActDailySpecialInfoResponse.newBuilder();
		
		ActiveModule_每日特惠 am = ActiveModule_每日特惠.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		Map<String, acttimelimit> execls = ActDataCenter.getAllData(acttimelimit.class, playercode);

		HashMap<Integer, Integer> map = am.getIdmap();
		for(int id : map.keySet()) {
			acttimelimit execl = execls.get(String.valueOf(id));
			
			Collection<Currency> curs = Currency.create(execl.getRewardtype(), execl.getRewardid(), execl.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			
			int canget = am.getBuygoodsids().contains(execl.getChargeid()) ? DrawStatus.hadget.getStatus() : am.getLockordermap().containsKey(execl.getChargeid())
					? DrawStatus.cannotget.getStatus() : DrawStatus.canget.getStatus();
			
			ActiveMessage.DailySpecialInfo.Builder build = ActiveMessage.DailySpecialInfo.newBuilder(); //每日特惠信息
			build.setId(execl.getId()); //id
			build.setGoodsid(execl.getChargeid()); //付费id 对应礼包价格
			build.addAllCurs(items); // 奖励类型、道具id、数量
			build.setCanget(canget); // 标识：可以领取、已领取、不能领取

			build.setRebate(execl.getRebate()); //返利倍数
			
			response.addDss(build.build());
		}

		DateTime end = ActiveUtil.time24(DateTime.now());
		response.setEnd(end.getMillis());
		send(messageSource, response, Result.Success);
	}
	
	private void send(MessageSource messageSource, ActiveMessage.ActDailySpecialInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
